/**
 * 
 */
package com.seguriapp.communication;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;

import com.seguriapp.R;

/**
 * @author pJohnson
 * 
 */
public class WebServiceConsumer {

	public static final boolean METHOD_GET = true;
	public static final boolean METHOD_POST = false;
	final private transient Context mContext;
	private transient String serverURL;
	private static WebServiceConsumer instance;
	private final Resources resources;

	/**
	 * Singleton method to get the instance of the class
	 * 
	 * @param mContext
	 *            the context
	 * @return the instance of the class
	 */
	public static WebServiceConsumer getInstance(final Context mContext) {
		if (instance == null) {
			instance = new WebServiceConsumer(mContext.getApplicationContext());
		}
		return instance;
	}

	private WebServiceConsumer(final Context context) {
		mContext = context;
		resources = this.mContext.getResources();
		serverURL = resources.getString(R.string.DefaultServer);

	}

	/**
	 * Register the selected complaint
	 * 
	 * @return a string with the servers answer.
	 */
	public void sendComplaint(final JSONObject complaint) {
		final String urlToConsume = serverURL
				+ resources.getString(R.string.regComplaint);
		JSONArray mediaJsonArray = new JSONArray();
		try {
			mediaJsonArray = complaint.optJSONArray("media");
			complaint.remove("media");

			final String rpta = RequestHttp.httpResponseString(urlToConsume,
					METHOD_POST, complaint.toString());

			if ((!rpta.contains("false")) && (mediaJsonArray != null)
					&& mediaJsonArray.length() > 0) {
				Log.e("Complaint Id", rpta);
				for (int i = 0; i < mediaJsonArray.length(); i++) {
					JSONObject media = (JSONObject) mediaJsonArray.get(i);
					media.put("complaintId", rpta);
					final String mediaContentUrl = serverURL
							+ resources.getString(R.string.regMediaContent);
					int serverResponse = HttpMediaRequest.upLoad2Server(media,
							mediaContentUrl);
					Log.e("Server response", String.valueOf(serverResponse));
				}

				// RequestHttp.httpResponseString(mediaContentUrl, METHOD_POST,
				// mediaJson.toString());
			}
		} catch (JSONException e) {
			Log.e("Json Error", e.getMessage());
		}

	}

	/**
	 * Registra la señal de alerta
	 * 
	 * @param mSignal
	 * @return
	 */
	public String sendSignal(final JSONObject mSignal) {
		final String urlToConsume = serverURL
				+ resources.getString(R.string.regAlert);
		return RequestHttp.httpResponseString(urlToConsume, METHOD_POST,
				mSignal.toString());

	}

	/**
	 * Obtiene la actividad criminal de la zona
	 * 
	 * @param postData
	 * @return
	 */
	public String getCrimeMapActivity(final JSONObject postData) {
		final String urlToConsume = serverURL
				+ resources.getString(R.string.listCrimeActivity);
		return RequestHttp.httpResponseString(urlToConsume, METHOD_POST,
				postData.toString());
	}

	/**
	 * Obtiene las categorias de los crimenes
	 * 
	 * @return
	 */
	public String getCrimeCategories() {
		final String urlToConsume = serverURL
				+ resources.getString(R.string.categoryList);
		return RequestHttp.httpResponseString(urlToConsume, METHOD_GET, null);
	}

	/**
	 * 
	 * @return
	 */
	public String getEntityCategories() {
		final String urlToConsume = serverURL
				+ resources.getString(R.string.entityTypeList);
		return RequestHttp.httpResponseString(urlToConsume, METHOD_GET, null);
	}

	/**
	 * 
	 * @param postObject
	 * @return
	 */
	public String getEntities(final JSONObject postObject) {
		final String urlToConsume = serverURL
				+ resources.getString(R.string.listEntitiesCategorically);
		return RequestHttp.httpResponseString(urlToConsume, METHOD_POST,
				postObject.toString());
	}

	public String getTramites() {
		final String urlToConsume = serverURL
				+ resources.getString(R.string.tramitesList);
		return RequestHttp.httpResponseString(urlToConsume, METHOD_GET, null);
	}

	public String getTramiteFile(String mGetPath) {
		final String urlToConsume = serverURL
				+ resources.getString(R.string.tramitesListGetPaperworkFile)+mGetPath;
		return RequestHttp.httpResponseString(urlToConsume, METHOD_GET, null);
	}
}
